<?php
class m_keuangan_adik_asuh extends Model{
    function getCountRows($params){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where ka.tanggal between '$params[awal]' and '$params[akhir]'";
        }
        if($params['idAdikAsuh'] != ""){
            $where .= " and ka.id_adik_asuh='$params[idAdikAsuh]'";
        }
        $query = $this->db->query("select ka.* from keuangan_adik_asuh ka $where");
        
        return $query->num_rows();
    }
    function getListKeuanganAdikAsuh($offset = NULL,$limit = NULL,$params){
        $where = "";
        if($params['awal'] != "" && $params['akhir']){
            $where .= "where ka.tanggal between '$params[awal]' and '$params[akhir]'";
        }
        if($params['idAdikAsuh'] != ""){
            $where .= " and ka.id_adik_asuh='$params[idAdikAsuh]'";
        }
        if($offset != NULL || $limit != NULL){
            $limits = "limit $offset,$limit";
        }else $limits = '';
        
        $query = $this->db->query("select ka.tanggal,ka.debet,ka.kredit,ka.saldo,ka.keterangan,m.nama as petugas,a.nama as adik_asuh,s.nama as sekolah,k.nama as kelas,ta.nama as tahun_ajaran,sms.nama as semester from keuangan_adik_asuh ka
        join member m on ka.id_petugas = m.id
        join adik_asuh a on ka.id_adik_asuh = a.id
        join sekolah s on ka.id_sekolah = s.id
        join kelas k on ka.id_kelas = k.id
        join tahun_ajaran ta on ka.id_tahun_ajaran = ta.id
        join semester sms on ta.id_semester = sms.id $where order by ka.tanggal desc $limits");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
    function getKeuanganAdikAsuhById($id){
        $query = $this->db->query("select da.alamat,ka.tanggal,ka.debet,ka.kredit,ka.saldo,ka.keterangan,m.nama as petugas,a.nama as adik_asuh,s.nama as sekolah,k.nama as kelas,ta.nama as tahun_ajaran,sms.nama as semester from keuangan_adik_asuh ka
        join member m on ka.id_petugas = m.id
        join adik_asuh a on ka.id_adik_asuh = a.id
        join dinamis_adik_asuh da on a.id = da.id_adik_asuh        
        join sekolah s on ka.id_sekolah = s.id
        join kelas k on ka.id_kelas = k.id
        join tahun_ajaran ta on ka.id_tahun_ajaran = ta.id
        join semester sms on ta.id_semester = sms.id where da.status = '1' and ka.id = '$id'");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
    function getSaldoAdikAsuh($idAdikAsuh){
        $query = $this->db->query("select saldo 
        from keuangan_adik_asuh  where id = (select max(id) from keuangan_adik_asuh where id_adik_asuh = '$idAdikAsuh')");
        if($query->num_rows() > 0){
            return $query->result_array();
        }else return array();
    }
}
?>
